package com.shanghaionstar.notifysetting.presenter;

import android.content.Context;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.response.DataPackageEntity;
import com.onstar.cn.notify.NotifyConfigRes;
import com.onstar.cn.notify.ValidateNotifyConfigRes;
import com.onstar.cn.smart.BaseResponse;
import com.onstar.cn.smart.entity.ServiceItem;
import com.shanghaionstar.carmanage.service.CarFlowDataService;
import com.shanghaionstar.notifysetting.iview.INotifyView;
import com.shanghaionstar.notifysetting.service.NotifySettingService;
import com.shanghaionstar.user.service.OptService;

import java.util.List;

import rx.functions.Action1;

/**
 * Created by Rengui Lu on 2017/3/21.
 */

public class NotifyPresenter extends BasePresenter {
    private IMVPView mOvdView;
    private NotifySettingService mService;
    private Context mContext;


//    驾驶行为：SMARTDRIVER
//    油耗排名：OILRANK
//    车联保险：UBI

    public interface IBType {
        String TYPE_TAN = "TAN";
        String TYPE_OVD = "OVD";
        String TYPE_DA = "DIAGNOSTICALERT";
        String TYPE_DS = "SMARTDRIVER";
        String TYPE_VI = "UBI";
        String TYPE_OL = "FUELECONOMY";
        String TYPE_COMMON = "COMMON";
        String TYPE_CHANGE_EMAIL = "CHANGE_EMAIL";
        String TYPE_CHANGE_PHONE = "CHANGE_PHONE";

        String CHANGE_FROM_NOTIFY = "notify";
        String DEST_TYPE_S = "S";
        String DEST_TYPE_E = "E";

    }

    public NotifyPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView view) {
        super.setView(view);
        mOvdView = view;
        mService = new NotifySettingService(view.onActivity());
        mContext = mOvdView.onActivity();
    }

    public void getNotifyConfig(String type) {
        mOvdView.setLoading(true);
        subscription = mService.getNotifyConfig(type)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<NotifyConfigRes>() {
                    @Override
                    public void call(NotifyConfigRes notifyConfigRes) {
                        if (mContext != null) {
                            mOvdView.setLoading(false);
                            if (CommonUtil.isNull(notifyConfigRes) && mOvdView instanceof INotifyView)
                                ((INotifyView) mOvdView).onGetNotifyConfigFailed();
                            else
                                ((INotifyView) mOvdView).onGetNotifyConfig(notifyConfigRes);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if (mContext != null && throwable instanceof OnStarException) {
                            mOvdView.setLoading(false);
                            ((INotifyView) mOvdView).onResponseError((OnStarException) throwable);
                        }
                    }
                });
    }

    public void setNotifyConfig(NotifyConfigRes req) {
        mOvdView.setLoading(true);
        subscription = mService.setNotifyConfig(req)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<BaseResponse>() {
                               @Override
                               public void call(BaseResponse baseResponse) {
                                   if (mContext == null)
                                       return;
                                   mOvdView.setLoading(false);
                                   mOvdView.onReponseSuccess(baseResponse);
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                if (mContext != null && throwable instanceof OnStarException) {
                                    mOvdView.setLoading(false);
                                    mOvdView.onResponseError((OnStarException) throwable);
                                }
                            }
                        });
    }

    public void validateNotifyConfig() {
        mOvdView.setLoading(true);
        subscription = mService.validateNotifyConfig()
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<ValidateNotifyConfigRes>() {
                               @Override
                               public void call(ValidateNotifyConfigRes validateNotifyConfigRes) {
                                   if (mContext == null)
                                       return;
                                   mOvdView.setLoading(false);
                                   if (validateNotifyConfigRes != null) {
                                       mOvdView.onReponseSuccess(validateNotifyConfigRes);
                                   }
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                if (mContext != null && throwable instanceof OnStarException) {
                                    mOvdView.setLoading(false);
                                    mOvdView.onResponseError((OnStarException) throwable);
                                }
                            }
                        });
    }


    public void getFlowData() {
        mOvdView.setLoading(true);
        CarFlowDataService carFlowDataService = new CarFlowDataService(mContext);
        subscription = carFlowDataService.doGetFlowData()
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<DataPackageEntity>() {
                    @Override
                    public void call(DataPackageEntity entity) {
                        if (mContext == null)
                            return;
                        mOvdView.setLoading(false);
                        if (CommonUtil.isNull(entity) && mOvdView instanceof INotifyView)
                            mOvdView.onResponseError(null);
                        else
                            ((INotifyView) mOvdView).onGetListPackage(entity);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (mContext != null) {
                            mOvdView.setLoading(false);
                            mOvdView.onResponseError((OnStarException) throwable);
                        }
                    }
                });
    }

    public void getDaServiceStatus(final Context context) {
        mOvdView.setLoading(true);
        OptService optService = new OptService(context);
        addSubscription(optService.getServiceStatus(IBType.TYPE_DA).subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<List<ServiceItem>>() {
                    @Override
                    public void call(List<ServiceItem> items) {
                        if (mContext == null)
                            return;
                        if (items == null || items.isEmpty()) {
                            ((INotifyView) mOvdView).onCheckStatusFailed(false);
                            return;
                        }
                        mOvdView.setLoading(false);
                        ServiceItem item = items.get(0);
                        if (!CommonUtil.isNull(item) && item.getOptStatus().equalsIgnoreCase("1")) {
                            mOvdView.onReponseSuccess(item);
                        } else {
//                            mOvdView.onReponseSuccess(null);
                            ((INotifyView) mOvdView).onCheckStatusFailed(false);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if (mContext != null) {
                            mOvdView.setLoading(false);
                            if (throwable instanceof OnStarException)
                                mOvdView.onResponseError((OnStarException) throwable);
                            ((INotifyView) mOvdView).onCheckStatusFailed(false);
                        }
                    }
                }));
    }


}
